from SimpleXMLRPCServer import SimpleXMLRPCServer
import xmlrpclib
import os
import os.path
import zlib


sharedir = 'data'
fileList = []

def getRootDir():
    return sharedir

def getFileList():
    rootdir = os.path.abspath(sharedir)
    fileList = []
    for parent, dirnames, filenames in os.walk(rootdir):
        for filename in filenames:
            fileList.append(os.path.join(parent,filename))

    fileList.sort()
    print(fileList)
    return fileList



def ishasfile(file, md5):
    return False


def getFile(file):
    try:
        os.stat(file)
    except(OSError):
        return
    hfile = open(file)
    data = ''
    try:
        data = xmlrpclib.Binary(hfile.read())
#data = zlib.compress(data, zlib.Z_BEST_COMPRESSION)
    finally:
        hfile.close()
    return data


server = SimpleXMLRPCServer(("0.0.0.0", 9000), allow_none=True)
print ("Listening on port 9000...")
server.register_function(getRootDir, 'getRootDir')
server.register_function(getFileList, 'getFileList')
server.register_function(getFile, 'getFile')
server.serve_forever()

